#include "CAppStateScores.h"

#include "CAppStateManager.h"

CAppStateScores CAppStateScores::Instance;

CAppStateScores::CAppStateScores() {
    CScore::Score.Target = -1;
    Surf_BG = NULL;
    Surf_ArrowL = NULL;
    Surf_ArrowR = NULL;
    Surf_Easy = NULL;
    Surf_Medium = NULL;
    Surf_Hard = NULL;
}

void CAppStateScores::OnActivate() {
    // Load Simple Logo
    Surf_BG = CSurface::OnLoad("content/image/record_screen.jpg");
    Surf_ArrowL = CSurface::OnLoad("content/image/arrow_left.jpg");
    Surf_ArrowR = CSurface::OnLoad("content/image/arrow_right.jpg");
    Surf_Easy = CSurface::OnLoad("content/image/Button/easy_pic_small.jpg");
    Surf_Medium = CSurface::OnLoad("content/image/Button/med_pic_small.jpg");
    Surf_Hard = CSurface::OnLoad("content/image/Button/hell_pic_small.jpg");
    Font = CFont::OnLoad("content/fonts/visitor2.ttf", 30);
    if (!CScore::Score.OnLoad("scores.xml"))
        printf("failed");
}

void CAppStateScores::OnDeactivate() {
    CScore::Score.Target = -1;
    SDL_FreeSurface(Surf_BG);
    SDL_FreeSurface(Surf_ArrowL);
    SDL_FreeSurface(Surf_ArrowR);
    SDL_FreeSurface(Surf_Easy);
    SDL_FreeSurface(Surf_Medium);
    SDL_FreeSurface(Surf_Hard);
}

void CAppStateScores::OnLoop() {
}

void CAppStateScores::OnRender(SDL_Surface* Surf_Display) {
    CSurface::OnDraw(Surf_Display, Surf_BG, 0, 0);

    switch (CScore::Score.Level) {
        case 0:
        {
            CSurface::OnDraw(Surf_Display, Surf_Medium, Surf_Display->w - 100, 36);
            CSurface::OnDraw(Surf_Display, Surf_ArrowR, 468, 52);
        }
        break;
        case 1:
        {
            CSurface::OnDraw(Surf_Display, Surf_Easy, 24, 36);
            CSurface::OnDraw(Surf_Display, Surf_Hard, Surf_Display->w - 100, 36);
            CSurface::OnDraw(Surf_Display, Surf_ArrowL, 120, 52);
            CSurface::OnDraw(Surf_Display, Surf_ArrowR, 468, 52);
        }
        break;
        case 2:
        {
            CSurface::OnDraw(Surf_Display, Surf_Medium, 24, 36);
            CSurface::OnDraw(Surf_Display, Surf_ArrowL, 120, 52);
        }
        break;
    }

    if (CScore::Score.Anim)
        CScore::Score.Animate(Surf_Display, Font);
    else
        CScore::Score.OnDraw(Surf_Display, Font);
}

CAppStateScores* CAppStateScores::GetInstance() {
    return &Instance;
}

void CAppStateScores::OnKeyDown(SDLKey sym, SDLMod mod, Uint16 unicode) {
    if (!CScore::Score.Anim) {
        switch (sym) {
            case SDLK_ESCAPE:
                CAppStateManager::SetActiveAppState(APPSTATE_MENU);
            break;

            case SDLK_UP:
                CScore::Score.ScrollUp();
            break;

            case SDLK_DOWN:
                CScore::Score.ScrollDown();
            break;

            case SDLK_RIGHT:
                CScore::Score.NextLevel();
            break;

            case SDLK_LEFT:
                CScore::Score.ExLevel();
            break;

            default: break;
        }
    }
}

void CAppStateScores::OnMouseWheel(bool Up, bool Down) {
    if (!CScore::Score.Anim) {
        if (Up)
            CScore::Score.ScrollUp();
        if (Down)
            CScore::Score.ScrollDown();
    }
}
